package javolution.io;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.Writer;
import java.nio.ByteBuffer;
import javolution.lang.Reusable;
import okio.Utf8;
import org.apache.commons.net.telnet.TelnetCommand;

/* loaded from: classes2.dex */
public final class UTF8ByteBufferWriter extends Writer implements Reusable {
    private ByteBuffer _byteBuffer;
    private char _highSurrogate;

    private void write2(int i) throws IOException {
        ByteBuffer byteBuffer;
        int i2;
        ByteBuffer byteBuffer2;
        int i3;
        ByteBuffer byteBuffer3;
        int i4;
        ByteBuffer byteBuffer4;
        int i5;
        if ((i & (-2048)) == 0) {
            byteBuffer4 = this._byteBuffer;
            i5 = (i >> 6) | 192;
        } else {
            if (((-65536) & i) == 0) {
                byteBuffer3 = this._byteBuffer;
                i4 = (i >> 12) | 224;
            } else {
                if (((-14680064) & i) == 0) {
                    byteBuffer2 = this._byteBuffer;
                    i3 = (i >> 18) | 240;
                } else {
                    if (((-201326592) & i) == 0) {
                        byteBuffer = this._byteBuffer;
                        i2 = (i >> 24) | TelnetCommand.EL;
                    } else if ((Integer.MIN_VALUE & i) != 0) {
                        throw new CharConversionException("Illegal character U+" + Integer.toHexString(i));
                    } else {
                        this._byteBuffer.put((byte) ((i >> 30) | 252));
                        byteBuffer = this._byteBuffer;
                        i2 = ((i >> 24) & 63) | 128;
                    }
                    byteBuffer.put((byte) i2);
                    byteBuffer2 = this._byteBuffer;
                    i3 = ((i >> 18) & 63) | 128;
                }
                byteBuffer2.put((byte) i3);
                byteBuffer3 = this._byteBuffer;
                i4 = ((i >> 12) & 63) | 128;
            }
            byteBuffer3.put((byte) i4);
            byteBuffer4 = this._byteBuffer;
            i5 = ((i >> 6) & 63) | 128;
        }
        byteBuffer4.put((byte) i5);
        this._byteBuffer.put((byte) ((i & 63) | 128));
    }

    @Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this._byteBuffer != null) {
            reset();
        }
    }

    @Override // java.io.Writer, java.io.Flushable
    public void flush() throws IOException {
        if (this._byteBuffer == null) {
            throw new IOException("Writer closed");
        }
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._byteBuffer = null;
        this._highSurrogate = (char) 0;
    }

    public UTF8ByteBufferWriter setByteBuffer(ByteBuffer byteBuffer) {
        return setOutput(byteBuffer);
    }

    public UTF8ByteBufferWriter setOutput(ByteBuffer byteBuffer) {
        if (this._byteBuffer == null) {
            this._byteBuffer = byteBuffer;
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public void write(char c) throws IOException {
        if (c < 55296 || c > 57343) {
            write((int) c);
        } else if (c < 56320) {
            this._highSurrogate = c;
        } else {
            write(((this._highSurrogate - 55296) << 10) + (c - Utf8.LOG_SURROGATE_HEADER) + 65536);
        }
    }

    @Override // java.io.Writer
    public void write(int i) throws IOException {
        if ((i & (-128)) == 0) {
            this._byteBuffer.put((byte) i);
        } else {
            write2(i);
        }
    }

    public void write(CharSequence charSequence) throws IOException {
        int length = charSequence.length();
        int i = 0;
        while (i < length) {
            int i2 = i + 1;
            char charAt = charSequence.charAt(i);
            if (charAt < 128) {
                this._byteBuffer.put((byte) charAt);
            } else {
                write(charAt);
            }
            i = i2;
        }
    }

    @Override // java.io.Writer
    public void write(String str, int i, int i2) throws IOException {
        int i3 = i2 + i;
        while (i < i3) {
            int i4 = i + 1;
            char charAt = str.charAt(i);
            if (charAt < 128) {
                this._byteBuffer.put((byte) charAt);
            } else {
                write(charAt);
            }
            i = i4;
        }
    }

    @Override // java.io.Writer
    public void write(char[] cArr, int i, int i2) throws IOException {
        int i3 = i2 + i;
        while (i < i3) {
            int i4 = i + 1;
            char c = cArr[i];
            if (c < 128) {
                this._byteBuffer.put((byte) c);
            } else {
                write(c);
            }
            i = i4;
        }
    }
}
